package com.ruoyi.webshop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.webshop.domain.CategoryOne;
import com.ruoyi.webshop.domain.CategoryTwo;
import com.ruoyi.webshop.service.ICategoryOneService;
import com.ruoyi.webshop.service.ICategoryTwoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.webshop.domain.CategoryThree;
import com.ruoyi.webshop.service.ICategoryThreeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 三级分类Controller
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/webshop/CategoryThree")
public class CategoryThreeController extends BaseController
{
    @Autowired
    private ICategoryThreeService categoryThreeService;

    @Autowired
    private ICategoryTwoService categoryTwoService;

    @Autowired
    private ICategoryOneService categoryOneService;

    /**
     * 查询三级分类列表
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:list')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryThree categoryThree)
    {
        startPage();
        List<CategoryThree> list = categoryThreeService.selectCategoryThreeList(categoryThree);
        return getDataTable(list);
    }

    /**
     * 导出三级分类列表
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:export')")
    @Log(title = "三级分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryThree categoryThree)
    {
        List<CategoryThree> list = categoryThreeService.selectCategoryThreeList(categoryThree);
        ExcelUtil<CategoryThree> util = new ExcelUtil<CategoryThree>(CategoryThree.class);
        util.exportExcel(response, list, "三级分类数据");
    }

    /**
     * 获取三级分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:query')")
    @GetMapping(value = "/{cthrId}")
    public AjaxResult getInfo(@PathVariable("cthrId") Long cthrId)
    {
        return success(categoryThreeService.selectCategoryThreeByCthrId(cthrId));
    }

    /**
     * 新增三级分类
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:add')")
    @Log(title = "三级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryThree categoryThree)
    {
        return toAjax(categoryThreeService.insertCategoryThree(categoryThree));
    }

    /**
     * 修改三级分类
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:edit')")
    @Log(title = "三级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryThree categoryThree)
    {
        return toAjax(categoryThreeService.updateCategoryThree(categoryThree));
    }

    /**
     * 删除三级分类
     */
    @PreAuthorize("@ss.hasPermi('webshop:CategoryThree:remove')")
    @Log(title = "三级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cthrIds}")
    public AjaxResult remove(@PathVariable Long[] cthrIds)
    {
        return toAjax(categoryThreeService.deleteCategoryThreeByCthrIds(cthrIds));
    }


    //======================


    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<CategoryThree> list = categoryThreeService.selectCategoryThreeListAll();
        return success(list);
    }

    @GetMapping("/listCascader")
    public AjaxResult listCascader()
    {
        List<CategoryOne> oneList = categoryOneService.selectCategoryOneListAll();
        List<CategoryTwo> twoList = categoryTwoService.selectCategoryTwoListAll();
        List<CategoryThree> threeList = categoryThreeService.selectCategoryThreeListAll();

        List<Map<String, Object>> data = oneList
                // for
                .stream()
                // 循环体  peek / map  循环体,  peek 循环谁返回 谁,  map 指定返回对象
                .map(one -> {
                    Map<String, Object> oneMap = new HashMap<>();
                    oneMap.put("value", one.getConeId());
                    oneMap.put("label", one.getConeName());
                    // children
                    List<Map<String, Object>> c2list = twoList.stream()
                            // if
                            .filter(two -> two.getConeId().equals(one.getConeId()))
                            .map(two -> {
                                Map<String, Object> twoMap = new HashMap<>();
                                twoMap.put("value", two.getCtwoId());
                                twoMap.put("label", two.getCtwoName());
                                // children
                                List<Map<String, Object>> c3List = threeList.stream()
                                        .filter(thr -> thr.getCtwoId().equals(two.getCtwoId()))
                                        .map(thr -> {
                                            Map<String, Object> thrMap = new HashMap<>();
                                            thrMap.put("value", thr.getCthrId());
                                            thrMap.put("label", thr.getCthrName());
                                            return thrMap;
                                        })
                                        // 封装成指定 结构 toList()
                                        .collect(Collectors.toList());
                                twoMap.put("children", c3List);
                                return twoMap;
                            })
                            .collect(Collectors.toList());
                    oneMap.put("children", c2list);
                    return oneMap;
                })
                .collect(Collectors.toList());

        return success(data);
    }
}
